//
//  QMainHeadView.m
//  QoilPublic
//
//  Created by 杨凯 on 16/7/1.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QMainHeadView.h"

@implementation QMainHeadView{

    CGRect _selfRect;
    UIImageView *imageView1;
    UIImageView *imageView2;
    NSTimer *_timer;
}

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        _selfRect = frame;
        _imageData = [NSMutableArray array];
        [self createUI];
    }
    return self;
}

- (void)createUI{
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, CGRectGetMaxY(_selfRect))];
    _scrollView.bounces = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    _scrollView.userInteractionEnabled = YES;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.decelerationRate = 0.1;
    [self addSubview:_scrollView];
    
    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(_selfRect)-20-kStatusHeight,kScreenWidth,18)]; // 初始化mypagecontrol
    
    [_pageControl setCurrentPageIndicatorTintColor:[QColorUtility defaultBlueColor]];
    [_pageControl setPageIndicatorTintColor:[UIColor whiteColor]];
    _pageControl.hidesForSinglePage = YES;
    _pageControl.userInteractionEnabled = NO;
    _pageControl.currentPage = 0;
    [self addSubview:_pageControl];
    
    imageView1 = [[UIImageView alloc]init];
    imageView1.clipsToBounds = YES;
    [_scrollView addSubview:imageView1];
    
    imageView2 = [[UIImageView alloc]init];
    imageView2.clipsToBounds = YES;
    [_scrollView addSubview:imageView2];
    
    UITapGestureRecognizer *gest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(getsAction)];
    [self addGestureRecognizer:gest];
}

- (void)getsAction{
    if([self.delegate respondsToSelector:@selector(scrollIndexWith:)]){
        [self.delegate scrollIndexWith:_pageControl.currentPage];
    }
}

- (void)setImageData:(NSMutableArray *)imageData{
    
    if (imageData.count == 0) {
        return;
    }
    _imageData = imageData;
    //取消定时器
    [_timer invalidate];
    _timer = nil;
    _timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(runTimePage) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];

    [_scrollView setContentSize:CGSizeMake(kScreenWidth * ([_imageData count] + 2), CGRectGetMaxY(_selfRect))];
    [_scrollView setContentOffset:CGPointMake(kScreenWidth, 0)];
    
    _pageControl.numberOfPages = [_imageData count];
    for (int i = 0;i<[_imageData count];i++){
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.clipsToBounds = YES;
        imageView.userInteractionEnabled = YES;
        NSString *url = [_imageData objectAtIndex:i];
        [imageView sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"mainPlace_image"]];
        imageView.frame = CGRectMake((kScreenWidth * i) + kScreenWidth, 0, kScreenWidth, CGRectGetMaxY(_selfRect));
        [_scrollView addSubview:imageView];
    }
    
    NSString *url = [_imageData objectAtIndex:([_imageData count]-1)];
    [imageView1 sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"mainPlace_image"]];
    imageView1.userInteractionEnabled = YES;
    imageView1.frame = CGRectMake(0, 0, kScreenWidth, CGRectGetMaxY(_selfRect));
    
    NSString *url2 = [_imageData objectAtIndex:0];
    [imageView2 sd_setImageWithURL:[NSURL URLWithString:url2] placeholderImage:[UIImage imageNamed:@"mainPlace_image"]];

    imageView2.userInteractionEnabled = YES;
    imageView2.frame = CGRectMake((kScreenWidth * ([_imageData count] + 1)) , 0, kScreenWidth, CGRectGetMaxY(_selfRect));
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    [_timer setFireDate:[NSDate dateWithTimeInterval:5 sinceDate:[NSDate date]]];
    
    CGFloat pagewidth = self.scrollView.frame.size.width;
    int currentPage = ((self.scrollView.contentOffset.x)/pagewidth);
    
    if (currentPage==0){
        [self.scrollView scrollRectToVisible:CGRectMake(kScreenWidth * [_imageData count], 0,kScreenWidth,CGRectGetMaxY(_selfRect)) animated:NO];
        _pageControl.currentPage = _imageData.count-1;
    }else if (currentPage==([_imageData count]+1)){
        [self.scrollView scrollRectToVisible:CGRectMake(kScreenWidth, 0,kScreenWidth,CGRectGetMaxY(_selfRect)) animated:NO];
        _pageControl.currentPage = 0;
    }else{
        int pages = ((self.scrollView.contentOffset.x-kScreenWidth)/pagewidth);
        _pageControl.currentPage = pages;
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView{

    CGFloat pagewidth = self.scrollView.frame.size.width;
    int currentPage = ((self.scrollView.contentOffset.x)/pagewidth);
    
    if (currentPage==0){
        [self.scrollView scrollRectToVisible:CGRectMake(kScreenWidth * [_imageData count], 0,kScreenWidth,CGRectGetMaxY(_selfRect)) animated:NO];
        _pageControl.currentPage = _imageData.count-1;
    }else if (currentPage==([_imageData count]+1)){
        [self.scrollView scrollRectToVisible:CGRectMake(kScreenWidth, 0,kScreenWidth,CGRectGetMaxY(_selfRect)) animated:NO];
        _pageControl.currentPage = 0;
    }
}

- (void)runTimePage{
    int page = (int)_pageControl.currentPage;
    page++;
    [self.scrollView scrollRectToVisible:CGRectMake(kScreenWidth*(page+1),0,kScreenWidth,CGRectGetMaxY(_selfRect)) animated:YES];
    if(page == _imageData.count){
        page = 0;
      _pageControl.currentPage = page;
    }else{
      _pageControl.currentPage = page;
    }
}

@end
